﻿using MediatR;
using Sowrk03_28.Repository;
using Swork03_28.Api.Application.Commands.SlaughterCommand;
using Swork03_28.Domain.TraceableInfos;
using Swork03_28.Services.Impls;

namespace Swork03_28.Api.Application.CommandHandlers.SlaughterCommandHandler
{
    /// <summary>
    /// 添加屠宰公司信息
    /// </summary>
    public class FirmCreateCommandHandler : IRequestHandler<FirmsCreateCommand, int>
    {
        private readonly IRepository<FirmInfo> _firminfo;

        public FirmCreateCommandHandler(IRepository<FirmInfo> _firminfo)
        {
            this._firminfo = _firminfo;
        }

        public async Task<int> Handle(FirmsCreateCommand request, CancellationToken cancellationToken)
        {
            request.FirmType = FirmType.屠宰公司;
            var data = await _firminfo.GetValues(x => x.BatchId == request.BatchId && x.FirmType == request.FirmType);
            var info = data.FirstOrDefault();
            if (info == null)
            {
                var res = new FirmInfo()
                {
                    BatchId = request.BatchId,
                    FirmType = FirmType.屠宰公司,
                    Address = request.Address,
                    CampanyImage = request.CampanyImage,
                    CampanyIntro = request.CampanyIntro,
                    CompanyName = request.CompanyName,
                    FoundingTime = request.FoundingTime,
                    Principal = null,
                };

                return await _firminfo.CreateAsync(res);
            }
            else
            {
                info.CampanyIntro = request.CampanyIntro;
                info.CompanyName = request.CompanyName;
                info.CampanyImage = request.CampanyImage;
                info.Address = request.Address;
                info.FoundingTime = request.FoundingTime;
                info.Id = request.Id;
                info.BatchId = request.BatchId;
                info.FirmType = request.FirmType;
                info.Id = request.Id;
                return await _firminfo.UpdateAsync(info);
            }
        }

    }
    /// <summary>
    /// 添加屠宰公司图片
    /// </summary>
    public class FirmImgCreateCommandHandler : IRequestHandler<FirmImgsCreateCommand, int>
    {
        private readonly IRepository<FirmInfo> _firminfoRepository;

        public FirmImgCreateCommandHandler(IRepository<FirmInfo> firminfoRepository)
        {
            _firminfoRepository = firminfoRepository;
        }

        public async Task<int> Handle(FirmImgsCreateCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var info = await _firminfoRepository.GetValues(x => x.BatchId == request.BatchId && x.FirmType == FirmType.屠宰公司);
            var res = info.FirstOrDefault();
            if (res != null)
            {
                res.CampanyImage = (res.CampanyImage + "," + request.Imgs).TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
                count += await _firminfoRepository.UpdateAsync(res);
            }
            else
            {
                var imgs = ("," + request.Imgs).TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
                var firminfo = new FirmInfo()
                {
                    BatchId = request.BatchId,
                    FirmType = FirmType.屠宰公司,
                    CampanyImage = request.Imgs,
                    Address = null,
                    CampanyIntro = null,
                    CompanyName = null,
                    Principal = null,
                    Quantity = null,
                    FoundingTime = null,
                };
                count += await _firminfoRepository.CreateAsync(firminfo);
            }
            return count;
        }
    }

    /// <summary>
    /// 获取屠宰公司信息
    /// </summary>
    public class FirmImgSelectCommandHandler : IRequestHandler<FirmSelectsCommand, FirmInfo>
    {
        private readonly IRepository<FirmInfo> repository;

        public FirmImgSelectCommandHandler(IRepository<FirmInfo> repository)
        {
            this.repository = repository;
        }

        public async Task<FirmInfo> Handle(FirmSelectsCommand request, CancellationToken cancellationToken)
        {
            var info = await repository.GetValues(x => x.BatchId == request.BatchId && x.FirmType == FirmType.屠宰公司);
            var res = info.FirstOrDefault();
            return res;
        }
    }

    /// <summary>
    /// 删除屠宰公司图片
    /// </summary>
    public class FirmImgDelCommandHandler : IRequestHandler<FirmImgDelCommand, int>
    {
        private readonly IRepository<FirmInfo> _firmInfo;

        public FirmImgDelCommandHandler(IRepository<FirmInfo> firmInfo)
        {
            _firmInfo = firmInfo;
        }

        public async Task<int> Handle(FirmImgDelCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var info = await _firmInfo.GetValues(x => x.BatchId == request.BatchId && x.FirmType == FirmType.屠宰公司);
            var Imgs = info.FirstOrDefault().CampanyImage.Split(",").ToList();
            Imgs.RemoveAll(x => x == request.Img);
            info.FirstOrDefault().CampanyImage = string.Join(",", Imgs);
            count += await _firmInfo.UpdateAsync(info.FirstOrDefault());
            return count;
        }
    }
}
